﻿using Lazy.Captcha.Core.Interface;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Helper.Config;

namespace Lazy.Captcha.Core.Config
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class RateLimitConfig : IRateLimitConfig
    {
        public RateLimitConfig ( IConfigCollect config )
        {
            IConfigSection section = config.GetSection("Hr:VerCode:limit");
            section.AddRefreshEvent(this._Init);
        }
        private void _Init ( IConfigSection section, string name )
        {
            this.Enabled = section.GetValue("Enabled", true);
            int sec = section.GetValue<int>("WindowSeconds", 60);
            this.WindowSeconds = new TimeSpan(0, 0, sec);
            this.MaxRequests = section.GetValue("MaxRequests", 10);
        }
        /// <summary>
        /// 是否启用频率限制
        /// </summary>
        public bool Enabled { get; private set; } = true;

        /// <summary>
        /// 时间窗口（秒）
        /// </summary>
        public TimeSpan WindowSeconds { get; private set; }

        /// <summary>
        /// 时间窗口内最大请求次数
        /// </summary>
        public int MaxRequests { get; set; } = 5;
    }
}
